﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Threading;
using System.Text;
using System.Data;
using System.Collections;
namespace Golf.WebUI
{
    public partial class Left_Hand : System.Web.UI.Page
    {
        /// <summary>
        /// 页码数
        /// </summary>
        public int Pager
        {
            get
            {
                return int.Parse(ViewState["page"].ToString());
            }
            set
            {
                ViewState["page"] = value;
            }
        }
        /// <summary>
        /// 分页 数字编号
        /// </summary>
        public string _UrlPage = "";

        /// <summary>
        /// 当前的商品类型名称
        /// </summary>
        public string _TypeName = "";
        /// <summary>
        ///   价格查询  促销价一
        /// </summary>
        public decimal _Pro_PromotionPrice_1;
        /// <summary>
        ///    价格查询  促销价二 
        /// </summary>
        public decimal _Pro_PromotionPrice_2;
        private Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //默认页码数为0
                Pager = 0;
                Bind_allType();
                //URL分页
                BindToRepeater();
            }

        }

        #region  页面顶上左边 和页面右边顶上 绑定的类型
        /// <summary>
        /// 绑定类型
        /// </summary>
        public void Bind_allType()
        {
            Golf.BLL.Golf_ProductType bll_type = new Golf.BLL.Golf_ProductType();
            DataSet ds = bll_type.GetList(" isshow=1 and IsParent=0"); DataTable dt = ds.Tables[0];
            rep_alltype.DataSource = ds;
            rep_alltype.DataBind();
        }
        #endregion
        #region  查询商品
        /// <summary>
        /// 商品id
        /// </summary>
        public string attr_for_proid = "";
        /// <summary>
        /// URL分页
        /// </summary>
        public void BindToRepeater()
        {
            List<Golf.Model.Golf_Product> _list = new List<Golf.Model.Golf_Product>();
            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();

            //每页显示像素
            int myPageSize = this.DevNetPager1.PageSize;
            //当前显示页的索引
            int myPageIndex = this.DevNetPager1.PageIndex;
            string url = HttpContext.Current.Request.Url.AbsolutePath + "?page={0}";
            DevNetPager1.DevWebUrl = url.Substring(1, url.Length - 1);
            DevNetPager2.DevWebUrl = url.Substring(1, url.Length - 1);
            DataSet ds;
            if (Pro_strids() == "") return;
            if (_Pro_PromotionPrice_1 == 0 && _Pro_PromotionPrice_2 == 0)
                ds = bll.GetList(myPageSize, myPageIndex, "  id in(" + Pro_strids() + ")");
            else
                ds = bll.GetList(myPageSize, myPageIndex, "  id in(" + Pro_strids() + ") and Pro_PromotionPrice>" + _Pro_PromotionPrice_1 + " and Pro_PromotionPrice<" + _Pro_PromotionPrice_2 + "");
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(ds.Tables[0]);
            foreach (var item in list)
            {
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            _list = list;
            int rowCount = (int)ds.Tables[2].Rows[0][0];//获取第二章表的集合数据 
            DevNetPager1.RecordCount = rowCount;//
            DevNetPager2.RecordCount = rowCount;//
            this.rep_proPage.DataSource = _list;
            this.rep_proPage.DataBind();
            Script_Write(_list);
        }
        #endregion

        #region  页面注册 js 脚本代码
        /// <summary>
        /// 生成页面脚本    鼠标移动到上面的
        /// </summary>
        public void Script_Write(List<Golf.Model.Golf_Product> list)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.Append(" <script language=\"JavaScript\">  \n");
            foreach (var item in list)
            {
                //ID，名称，名优产品，HREF，图片，CommercialMsg，价格，规格，保存，评价，视频，缺货，折扣
                //ID, Name, BrandName, Href, Image, CommercialMsg, Price, RRP, Save, Rating, Video, OOS, Discount
                sbScript.Append("MAD.products.addProduct(new MAD.entities.product(\"" + item.Id + "\", \" " + item.Pro_Name + "\", \"" + new Index().GetBrandName((int)item.Pro_BrandTypeId) + "\", \"ProductDetail.aspx?id=" + item.Id + "\", \"ImageProducts/" + item.Pro_PicBig.Split(',')[0] + "\", \"" + item.Pro_Name + "\", \"" + item.Pro_PromotionPrice + "\", \"" + item.Pro_MarketPrice + "\", \"" + item.Pro_Save + "\", \"" + item.Pro_Star + "\", \"1\")); \n");
            }
            sbScript.Append(" </script> \n");
            script_pro.InnerHtml += sbScript.ToString();
        }
        #endregion

        #region 得到 left  hand  商品的id编号
        /// <summary>
        /// 根据left hand  查询 商品id 字符串
        /// </summary>
        /// <returns></returns>
        public string Pro_strids()
        {
            Golf.BLL.Golf_ProAttrDetail bll = new Golf.BLL.Golf_ProAttrDetail();
            List<Golf.Model.Golf_ProAttrDetail> list = bll.GetModelList("','+DetaileId like '%,52,%'");
            StringBuilder sb = new StringBuilder();
            foreach (var item in list)
            {
                sb.Append(item.ProId + ",");
            }
            if (sb.Length > 0)
            {
                return sb.ToString().Substring(0, sb.Length - 1);
            }
            else
            {
                return sb.ToString();
            }
        }

        #endregion

        protected void txtLowerPriceBound_TextChanged(object sender, EventArgs e)
        {
            _Pro_PromotionPrice_1 = decimal.Parse(txtLowerPriceBound.Text);
            _Pro_PromotionPrice_2 = decimal.Parse(txtUpperPriceBound.Text);
            BindToRepeater();
            udpProducts.Update();
        }
    }
}
